package com.luck.easyfoodbatch.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.luck.easyfoodbatch.App;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.dialog.CustomDialog;
import com.luck.easyfoodbatch.mvp.model.Collect;
import com.luck.easyfoodbatch.mvp.model.RefundInfoM;
import com.luck.easyfoodbatch.mvp.order.DeleteOrderModel;
import com.luck.easyfoodbatch.mvp.order.DeleteOrderPresenter;
import com.luck.easyfoodbatch.mvp.order.DeleteOrderView;
import com.luck.easyfoodbatch.mvp.order.RefundInfoModel;
import com.luck.easyfoodbatch.mvp.order.RefundInfoPresenter;
import com.luck.easyfoodbatch.mvp.order.RefundInfoView;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Tools;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ReturnDetailsActivity extends BaseActivity implements RefundInfoView, DeleteOrderView {
    @BindView(R.id.tv_shopName)
    TextView tv_shopName;
    @BindView(R.id.iv_banner)
    ImageView iv_banner;
    @BindView(R.id.tv_goods_name)
    TextView tv_goods_name;
    @BindView(R.id.tv_spec)
    TextView tv_spec;
    @BindView(R.id.tv_money)
    TextView tv_money;
    @BindView(R.id.tv_sum_money)
    TextView tv_sum_money;
    @BindView(R.id.tv_yf_money)
    TextView tv_yf_money;
    @BindView(R.id.tv_order_money)
    TextView tv_order_money;
    @BindView(R.id.tv_sfk_money)
    TextView tv_sfk_money;
    @BindView(R.id.tv_delete)
    TextView tv_delete;
    @BindView(R.id.tv_status)
    TextView tv_status;
    @BindView(R.id.rl_shop)
    RelativeLayout rl_shop;
    @BindView(R.id.tv_order_sn)
    TextView tv_order_sn;
    @BindView(R.id.tv_time)
    TextView tv_time;
    @BindView(R.id.tv_refund_reason)
    TextView tv_refund_reason;
    @BindView(R.id.ll_refund_reason)
    LinearLayout ll_refund_reason;
    @BindView(R.id.tv_phone)
    TextView tv_phone;
    private String rid = "";
    private RefundInfoModel refundInfoModel;
    private String shop_id = "";
    private String shop_hx = "";
    private String shop_name = "";
    private String order_id = "";
    private DeleteOrderModel deleteOrderModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_return_details, "订单详情", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        refundInfoModel = new RefundInfoPresenter(this, mContext);
        rid = getIntent().getStringExtra("rid");
        showProgressDialog(getString(R.string.app_loading));
        refundInfoModel.requestRefundInfo(rid);
    }


    @OnClick({R.id.tv_delete, R.id.rl_shop, R.id.tv_phone})
    public void onClick(View view) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.tv_delete:
                // 删除订单
                showOrderDialog(1, order_id);
                deleteOrderModel = new DeleteOrderPresenter(ReturnDetailsActivity.this, mContext);
                break;
            case R.id.rl_shop:
                bundle.putString("shop_id", shop_id);
                bundle.putString("toChatUsername", shop_hx);
                startAct(ShopHomeActivity.class, bundle);
                break;
            case R.id.tv_phone:
//                bundle.putString(Constant.EXTRA_USER_ID, shop_hx);
//                bundle.putString(Constant.EXTRA_NICK_ID, shop_name);
//                intent.setClass(mContext, ChatActivity.class);
//                intent.putExtras(bundle);
//                mContext.startActivity(intent);
                showPhoneDialog();
                break;
        }
    }

    @Override
    public void refundInfoSuccess(RefundInfoM model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                RefundInfoM.DataBean data = model.getData();
                order_id = data.getRid() + "";
                shop_id = data.getShop_id() + "";
                shop_hx = data.getShop_hx();
                shop_name = data.getShop_name();
                String shop_name = data.getShop_name();
                String goods_avatar = data.getGoods_avatar();
                String goods_name = data.getGoods_name();
                int status = data.getStatus();
                String pay_time = data.getPay_time();
                if (!Tools.isNull(pay_time)) {
                    tv_time.setText(Tools.SubTime3(pay_time));
                } else {
                    tv_time.setText("");
                }
                String order_sn = data.getOrder_sn();
                String refund_reason = data.getRefund_reason();
                if (!Tools.isNull(order_sn)) {
                    tv_order_sn.setText(order_sn);
                } else {
                    tv_order_sn.setText("");
                }
                switch (status) {
                    case 1:
                        tv_status.setText("处理中");
                        break;
                    case 2:
                        tv_status.setText("已同意");
                        break;
                    case 3:
                        tv_status.setText("已拒绝");
                        if (!Tools.isNull(refund_reason)) {
                            ll_refund_reason.setVisibility(View.VISIBLE);
                            tv_refund_reason.setText(refund_reason);
                        }
                        break;
                    case 4:
                        tv_status.setText("退款成功");
                        break;
                }

                if (!Tools.isNull(shop_name)) {
                    tv_shopName.setText(shop_name);
                } else {
                    tv_shopName.setText("");
                }
                if (!Tools.isNull(goods_avatar)) {
                    String path = "";
                    if (goods_avatar.startsWith("http")) {
                        path = goods_avatar;
                    } else {
                        path = HttpUtil.IMG_THUMB_BASE_URL + goods_avatar;
                    }
                    Glide.with(getApplicationContext())
                            .load(path)
                            .centerCrop()
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .into(iv_banner);
                } else {
                    iv_banner.setImageResource(R.mipmap.loading_4b3);
                }
                tv_goods_name.setText(goods_name);
                tv_spec.setText("规格：" + data.getGoods_num() + data.getGoods_specifications() + "/" + data.getGoods_package());
                tv_money.setText("￥" + data.getPrice());
                tv_sum_money.setText("￥" + data.getOrder_price());
                tv_yf_money.setText("￥" + data.getFreight_fee());
                tv_order_money.setText("￥" + data.getOrder_price());
                tv_sfk_money.setText("￥" + data.getOrder_price());
            } else {
                T.showToast(mContext, model.getMsg());
            }
        }
    }

    @Override
    public void deleteOrderSuccess(Collect model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                setResult(RESULT_OK);
                finish();
            } else {
                T.showToast(mContext, model.getMsg());
            }
        }
    }

    @Override
    public void httpStatusError() {

    }

    @Override
    public void hideLoading() {
        hideShowProgress();
    }


    /**
     * 操作订单
     */
    private void showOrderDialog(final int type, final String orderId) {
        final CustomDialog dialog = new CustomDialog(mContext, App.screenWidth * 3 / 4,
                App.screenHeight / 4, R.layout.wind_base_dialog_xml, R.style.Theme_dialog);
        Button btn_cancel = (Button) dialog.findViewById(R.id.btn_cancel);
        Button btn_commit = (Button) dialog.findViewById(R.id.btn_commit);
        final TextView tv_content = (TextView) dialog.findViewById(R.id.tv_content);
        switch (type) {
            case 1:
                tv_content.setText("确定要删除该订单吗？");
                break;
        }

        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        btn_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showProgressDialog(getString(R.string.app_loading));
                switch (type) {
                    case 1:
                        showProgressDialog(getString(R.string.app_loading));
                        deleteOrderModel.requestDeleteOrder(1, orderId);
                        break;
                }
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    /**
     * 拨打电话
     */
    private void showPhoneDialog() {
        final CustomDialog dialog = new CustomDialog(mContext, App.screenWidth * 3 / 4,
                App.screenHeight / 4, R.layout.wind_base_dialog_xml, R.style.Theme_dialog);
        Button btn_cancel = (Button) dialog.findViewById(R.id.btn_cancel);
        Button btn_commit = (Button) dialog.findViewById(R.id.btn_commit);
        TextView tv_title = (TextView) dialog.findViewById(R.id.tv_title);
        TextView tv_content = (TextView) dialog.findViewById(R.id.tv_content);
        tv_title.setText("联系客服");
        btn_commit.setText("呼叫");
        tv_content.setText(HttpUtil.CALL_PHONE);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        btn_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Tools.callPhone(HttpUtil.CALL_PHONE, mContext);
                dialog.dismiss();
            }
        });
        dialog.show();
    }
}
